<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="${path}/static/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="${path}/static/css/font-awesome.min.css">
    <link rel="stylesheet" href="${path}/static/css/main.css">
    <link rel="stylesheet" href="${path}/static/css/doc.min.css">
    <link rel="stylesheet" href="${path}/static/ztree/zTreeStyle.css">
    <style>
        .tree li {
            list-style-type: none;
            cursor:pointer;
        }
    </style>
</head>

<body>

<%-- 顶部导航 --%>
<jsp:include page="${path}/static/nav.jsp"></jsp:include>

<div class="container-fluid">
    <div class="row">

        <%--  左侧导航栏  --%>
        <jsp:include page="${path}/static/left.jsp"></jsp:include>

        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">

            <div class="panel panel-default">
                <div class="panel-heading">
                    <i class="glyphicon glyphicon-th-list"></i> 权限菜单列表
                    <div style="float:right;cursor:pointer;" data-toggle="modal" data-target="#myModal"><i class="glyphicon glyphicon-question-sign"></i>
                    </div>
                </div>

                <div class="panel-body">
                    <ul id="treeDemo" class="ztree">

                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 增加用户模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">×
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    请填权限信息
                </h4>
            </div>
            <div class="modal-body">
                <form role="form" id="addPermissionForm">
                    <input type="hidden" id="hiddenPermissionId" name="permissionid" value="">
                    <div class="form-group">
                        <label for="permissionname">权限名称</label>
                        <input type="text" class="form-control" name="permissionname" id="permissionname" placeholder="权限名">
<%--                        <p class="help-block label label-warning">注意：用户名长度小于等于5</p>--%>
                    </div>
                    <div class="form-group">
                        <label for="permissionpath">权限地址</label>
                        <input type="text" class="form-control" name="permissionpath" id="permissionpath" placeholder="权限地址">
                        <p class="help-block label label-warning">注意：权限地址应为/开头,确保存在</p>
                    </div>

                    <button type="button" onclick="addPermission()" class="btn btn-success"><i class="glyphicon glyphicon-plus"></i> 新增</button>
                    <button type="button" class="btn btn-danger"><i class="glyphicon glyphicon-refresh"></i> 重置</button>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">关闭
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script src="${path}/static/jquery/jquery-2.1.1.min.js"></script>
<script src="${path}/static/bootstrap/js/bootstrap.min.js"></script>
<script src="${path}/static/script/docs.min.js"></script>
<script src="${path}/static/ztree/jquery.ztree.all-3.5.min.js"></script>
<script src="${path}/static/layer/layer.js"></script>
<script type="text/javascript">

    var setting={
        view: {
            selectedMulti: false,
            addDiyDom: function(treeId, treeNode){
                var icoObj = $("#" + treeNode.tId + "_ico"); // tId = permissionTree_1, $("#permissionTree_1_ico")
                if ( treeNode.icon ) {
                    icoObj.removeClass("button ico_docu ico_open").addClass("fa fa-fw " + treeNode.icon).css("background","");
                }

            },
            addHoverDom: function(treeId, treeNode){
                // console.log(treeNode);
                //   <a><span></span></a>
                var aObj = $("#" + treeNode.tId + "_a"); // tId = permissionTree_1, ==> $("#permissionTree_1_a")
                // aObj.attr("href", "javascript:;");
                if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
                var s = '<span id="btnGroup'+treeNode.tId+'">';
                if ( treeNode.level == 0 ) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs " data-toggle="modal" data-target="#myModal"  style="margin-left:10px;padding-top:0px;" onclick="savePermission('+treeNode.permissionid+')" >&nbsp;&nbsp;<i class="fa fa-fw fa-plus rbg "></i></a>';
                } else if ( treeNode.level == 1 ) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="updatePermission('+treeNode.permissionid+')" title="修改权限信息">&nbsp;&nbsp;<i class="fa fa-fw fa-edit rbg "></i></a>';
                    if (treeNode.children.length == 0) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="delPermission('+treeNode.permissionid+')" >&nbsp;&nbsp;<i class="fa fa-fw fa-times rbg "></i></a>';
                    }
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" data-toggle="modal" data-target="#myModal"  style="margin-left:10px;padding-top:0px;" onclick="savePermission('+treeNode.permissionid+')" >&nbsp;&nbsp;<i class="fa fa-fw fa-plus rbg "></i></a>';
                } else if ( treeNode.level == 2 ) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="updatePermission('+treeNode.permissionid+')" title="修改权限信息"  >&nbsp;&nbsp;<i class="fa fa-fw fa-edit rbg "></i></a>';
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="delPermission('+treeNode.permissionid+')" >&nbsp;&nbsp;<i class="fa fa-fw fa-times rbg "></i></a>';
                }

                s += '</span>';
                aObj.append(s);
            },
            removeHoverDom: function(treeId, treeNode){
                $("#btnGroup"+treeNode.tId).remove();
            }
        },
        async:{
            //是否异步请求
            enable: true,
            //数据地址
            url:"${path}/permission/list",
            //将permission作为参数查找子信息
            autoParam:["permissionid"]
        },
        callback: {
            onClick : function(event, treeId, json) {
            }
        }
    };

    $(function () {
        $(".list-group-item").click(function(){
            if ( $(this).find("ul") ) {
                $(this).toggleClass("tree-closed");
                if ( $(this).hasClass("tree-closed") ) {
                    $("ul", this).hide("fast");
                } else {
                    $("ul", this).show("fast");
                }
            }
        });

        showList();

    });
    
    function showList() {
        //使用zTree
        $.fn.zTree.init($("#treeDemo"),setting);
    }

    //在增加权限信息时将值传递给模态框
    function savePermission(permission){
        alert(permission);
        $("#hiddenPermissionId").val(permission);
    }
    //增加权限信息
    function addPermission(){
        $.ajax({
            url:"${path}/permission/save",
            method:"post",
            data:$("#addPermissionForm").serialize(),
            error:function (err) {
                alert("失败");
            },
            success:function (obj) {
                if (obj==true){
                    layer.msg("添加成功", {time:1000, icon:6, shift:3}, function(){
                        showList();
                    });
                }else{
                    layer.msg("添加失败", {time:1000, icon:5, shift:6});
                }
            }
        });
    }


    //跳转修改权限信息页面
    function updatePermission(permissionid){
        location.href = "${path}/permission/updateView?permissionid="+permissionid;
    }

    //删除权限
    function delPermission(permissionid){
        $.ajax({
            url:"${path}/permission/delete",
            method:"post",
            data:{"permissionid":permissionid},
            error:function (err) {
                alert("失败");
            },
            success:function (obj) {
                if (obj==true){
                    layer.msg("删除成功", {time:1000, icon:6, shift:3}, function(){
                        showList();
                    });
                }else{
                    layer.msg("删除失败", {time:1000, icon:5, shift:6});
                }
            }
        });
    }

</script>
</body>
</html>
